package com.zl.web.action;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.zl.domain.User;
import com.zl.service.UserService;

public class UserAction extends ActionSupport implements ModelDriven<User>{

	private static final long serialVersionUID = 1L;
	
	private User user = new User();


	@Override
	public User getModel() {
		// TODO Auto-generated method stub
		
		return user;
	}
	
	
	UserService userservice;
	public void setUserservice(UserService userservice) {
		this.userservice = userservice;
	}
	
	
	public String login(){
		
		User u = userservice.login(user);
		
		if (u == null ) {
			return LOGIN;
		}else{
			
			ServletActionContext.getRequest().getSession().setAttribute("user", u);
			
			return SUCCESS;
		}
		
		
	}
	//安全退出
	public String exit(){
		
		//接收请求参数
		ServletActionContext.getRequest().getSession().removeAttribute("user");
		
		
		return LOGIN;
	}
	public String regist(){
		
		//接收请求参数
		userservice.save(user);
		
		
		return LOGIN;
	}
	//通过登录名判断登录名是否存在
	public String checkCode(){
		
	    User newuser =userservice.checkCode(user.getUser_code());
//	    System.out.println(user.getUser_code());
		//获取response
	    HttpServletResponse response = ServletActionContext.getResponse();
	    response.setContentType("text/html;chartset=utf-8");
	    try {
			
	    	if (newuser != null) {
	    		//登录名存在
	    		response.getWriter().print("fail");
	    		
	    	}else{
	    		//登录名不存在
	    		response.getWriter().print("success");
	    	}
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		return NONE;
	}

}
